package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jeecg.common.api.ding.DingApi;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.entity.DiyProjectProfessor;
import org.jeecg.modules.entity.DiyTableStatus;
import org.jeecg.modules.service.impl.DiyProjectProfessorServiceImpl;
import org.jeecg.modules.service.impl.ProjectOwnProfessorServiceImpl;
import org.jeecg.modules.vo.Project;
import org.jeecg.modules.vo.ProjectOwnProfessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

/**
 * @author : ShiJianlong
 * @date : 2020/7/9 15:10
 */
@Api(tags="项目拥有的专家状态")
@RestController
@RequestMapping("/org/projectOwnProfessor")
@Slf4j
public class ProjectOwnProfessorController {

    @Autowired
    private ProjectOwnProfessorServiceImpl projectOwnProfessorService;
    @Autowired
    private DiyProjectProfessorServiceImpl diyProjectProfessorService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DingApi dingApi;

    /**
     * 项目拥有的专家状态
     * @param pageNo
     * @param pageSize
     * @return
     */
    @AutoLog(value = "项目拥有的专家状态-分页列表查询")
    @ApiOperation(value="项目拥有的专家状态-分页列表查询", notes="项目拥有的专家状态-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   @RequestParam(value = "projectId",required = false) String projectId) {
        if(projectId==null){
            Result<Page<ProjectOwnProfessor>> result = new Result<>();
            Page<ProjectOwnProfessor> page = new Page<>(pageNo, pageSize);
//            Page<Project> page = new Page<>(pageNo, pageSize);
            page = projectOwnProfessorService.queryPage(page);//通知公告消息
            log.info("查询当前页：" + page.getCurrent());
            log.info("查询当前页数量：" + page.getSize());
            log.info("查询结果数量：" + page.getRecords().size());
            log.info("数据总数：" + page.getTotal());
            result.setSuccess(true);
            result.setResult(page);
            return result;
        }else{

            Result<Page<ProjectOwnProfessor>> result = new Result<>();
            Page<ProjectOwnProfessor> page = new Page<>(pageNo, pageSize);
            page = projectOwnProfessorService.queryByProjectId(projectId);//通知公告消息
            result.setSuccess(true);
            result.setResult(page);
            return result;

        }

    }
    @AutoLog(value = "项目拥有的专家状态-分页列表查询")
    @ApiOperation(value="项目拥有的专家状态-分页列表查询", notes="项目拥有的专家状态-分页列表查询")
    @GetMapping(value = "/list01/{shu}")
    public Result<?> queryPageList01(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                     @PathVariable("shu") String[] shu) {
        String status=shu[0];
        String projectName01=shu[1];
        String projectType01=shu[2];
        Result<Page<ProjectOwnProfessor>> result = new Result<>();
        Page<ProjectOwnProfessor> page = new Page<>(pageNo, pageSize);
        if("".equals(status)&&"".equals(projectName01)&&"".equals(projectType01)) {
            page = projectOwnProfessorService.queryPage(page);//通知公告消息
        }
        else if(!"".equals(status)&&!"".equals(projectName01)&&!"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage02(page, status,projectName01,projectType01);//通知公告消息
        }
        else if(!"".equals(status)&&"".equals(projectName01)&&"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage01(page,status);//通知公告消息
        }
        else if("".equals(status)&&!"".equals(projectName01)&&"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage03(page,projectName01);//通知公告消息
        }
        else if("".equals(status)&&"".equals(projectName01)&&!"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage04(page,projectType01);//通知公告消息
        }
        else if(!"".equals(status)&&!"".equals(projectName01)&&"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage05(page,status,projectName01);//通知公告消息
        }
        else if(!"".equals(status)&&"".equals(projectName01)&&!"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage06(page,status,projectType01);//通知公告消息
        }
        else if("".equals(status)&&!"".equals(projectName01)&&!"".equals(projectType01)){
            page = projectOwnProfessorService.queryPage07(page,projectName01,projectType01);//通知公告消息
        }
        log.info("查询当前页：" + page.getCurrent());
        log.info("查询当前页数量：" + page.getSize());
        log.info("查询结果数量：" + page.getRecords().size());
        log.info("数据总数：" + page.getTotal());
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }
    @AutoLog(value = "项目拥有的专家状态-分页列表查询")
    @ApiOperation(value="项目拥有的专家状态-分页列表查询", notes="项目拥有的专家状态-分页列表查询")
    @GetMapping(value = "/deleteOneProfessor/{projectId}/{dingUserId}/{deleteCause}")
    public Result<?> deleteOneProfessor(@PathVariable("projectId") String projectId,
                                        @PathVariable("dingUserId") String dingUserId,
                                        @PathVariable("deleteCause")String deleteCause,
                                        HttpServletRequest request){

        //更新改专家的状态为已被管理员删除
        diyProjectProfessorService.update(new UpdateWrapper<DiyProjectProfessor>().lambda().
                eq(DiyProjectProfessor::getProjectId,projectId).
                eq(DiyProjectProfessor::getDingUserId,dingUserId).
                set(DiyProjectProfessor::getProfessorInProjectStates,"-3").
                set(DiyProjectProfessor::getDeleteCause,deleteCause)
        );
        Subject subject = SecurityUtils.getSubject();
        LoginUser user = (LoginUser) subject.getPrincipal();

        //项目中考虑的人数减一

        //获取发起人id
        String originator_userid = user.getUsername();
        originator_userid=dingApi.getUnionIdByUserId(originator_userid);
        String url = "http://312730ua54.51vip.biz" + request.getContextPath() + "/projectProfessorInfo/randomOneChoose/" + projectId + "/" + dingUserId+"/"+
                originator_userid+"/"+false;
        System.out.println(url);
        restTemplate.delete(url);
        return Result.ok("删除成功");
    }

}
